# PROCESS DATA FOR LIGHTGBM
import pandas as pd
import numpy as np
import lightgbm as lgb
import random
import types
import os
import gc
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_absolute_error as mae
from datetime import datetime

def lgb_train(mall):
    print('\n%s predict start'%(mall))
    print("LGB Reading data from disk ...")
    prop = pd.read_csv(r'.\input\ccf_first_round_shop_info.csv')
    # prop = pd.read_csv(r'.\input\feature.csv')
    train = pd.read_csv(r'.\input\ccf_first_round_user_shop_behavior.csv')
    test =  pd.read_csv(r'.\input\AB-evaluation_public.csv')

    print("Processing data for LightGBM ...")

    for c in train.columns:
        train[c] = train[c].fillna(0)
    for c in train.columns:
        train[c] = train[c].fillna(0)

    prop = prop.drop(['category_id','longitude','latitude','price'],axis=1)

    df_train = train.merge(prop, how='left', on='shop_id')

    df_train = df_train.drop(['user_id','time_stamp', 'wifi_infos'],axis=1)

    test = test.drop(['time_stamp','wifi_infos'],axis=1)

    x_train = df_train[df_train.mall_id == mall]
    y_train = pd.DataFrame()
    y_train['shop_id'] = x_train['shop_id'].values
    lal = LabelEncoder()
    lal.fit(list(y_train['shop_id']))
    y_train['shop_num'] = lal.transform(list(y_train['shop_id'].values))
    train_y = y_train['shop_num'].values.astype(np.float64, copy=False)

    shop = list(set(y_train['shop_id']))
    print(' 店铺个数=%d'%(len(shop)))
    # if len(shop)>95:
    #     return mall
    x_train = x_train.drop(['shop_id','mall_id',],axis=1)
    x_train = x_train.values.astype(np.float64, copy=False)

    x_test = test[test.mall_id == mall]
    pred_result = pd.DataFrame()
    pred_result['row_id'] = x_test.row_id
    x_test = x_test.drop(['row_id','user_id','mall_id'], axis=1)

    print("",x_train.shape,train_y.shape)
    d_train = lgb.Dataset(x_train, label=train_y)

    # RUN LIGHTGBM
    params = {}
    params['max_bin'] = 255
    #params['max_depth'] = 9
    params['learning_rate'] = 0.1 
    params['boosting_type'] = 'gbdt'
    params['objective'] = 'multiclass'
    params['metric'] = 'multi_logloss'
    params['num_class'] = len(shop)
    #params['num_iterations'] = 100
    params['num_leaves'] = 31 
    params['verbose'] = 0

    np.random.seed(0)
    random.seed(0)

    print(" Fitting LightGBM model ...")
    clf = lgb.train(params, d_train, 10)

    print(" Start LightGBM prediction ...")
    p_test = clf.predict(x_test)
    p_test = np.argmax(p_test, axis=1)



    pred_result['shop_num'] = p_test
    #pred_result = pred_result.merge(y_train, how='left', on='shop_num')
    #pred_result = pred_result[['row_id','shop_id']]
    #print('row_id length = %d'%(len(pred_result['row_id'])))

    del x_train
    del x_test
    del y_train
    del train_y
    del p_test
    del clf
    del d_train
    del prop
    gc.collect()
    # 写结果到磁盘
    print(' write result to disk..')
    pred_result.to_csv(r'.\mall result\%s.csv'%(mall), index=False)
    print(' finish')
    gc.collect()
    return pred_result

def getMall():
    prop = pd.read_csv(r'.\input\ccf_first_round_shop_info.csv')
    # prop = pd.read_csv(r'.\input\feature.csv')
    train = pd.read_csv(r'.\input\ccf_first_round_user_shop_behavior.csv')
    test =  pd.read_csv(r'.\input\AB-evaluation_public.csv')

    for c in train.columns:
        train[c] = train[c].fillna(0)
    for c in train.columns:
        train[c] = train[c].fillna(0)

    prop = prop.drop(['category_id','longitude','latitude','price'],axis=1)

    df_train = train.merge(prop, how='left', on='shop_id')

    unique_mall = pd.DataFrame()
    unique_mall['mall_id'] = df_train['mall_id'].drop_duplicates()
    print('商场总数=%d'%(len(unique_mall)))
    print('记录商场id')
    unique_mall.to_csv(r'.\input\mall_id.csv', index=False)
    del prop
    del train
    del test
    del df_train
    del unique_mall
    gc.collect()

if __name__ == "__main__":
    #LGB 模型预测
    #getMall()
    # lgb_train('m_3005')
    # lgb_train('m_3839')
    # lgb_train('m_6587')

    test =  pd.read_csv(r'.\input\AB-evaluation_public.csv')
    result = pd.DataFrame()
    result['row_id'] = test['row_id']
    del test;gc.collect()
    
    mall_id = pd.read_csv(r'.\input\mall_id.csv')
    # for mall in mall_id['mall_id']:
    #     #if not os.path.exists(r'.\input\%s.csv'%(mall)):
    #     pred_result = lgb_train(mall)
    #     result = result.merge(pred_result, how='left', on='row_id')
    #     del pred_result;gc.collect()
    for mall in mall_id['mall_id']:
        pred = pd.read_csv(r'.\mall result\%s.csv'%(mall))
        result = result.merge(pred, how='left', on='row_id')
    result.to_csv(r'.\result\result.csv', index=False)